//
//  CLCustomNavView.m
//  Class_IOS
//
//  Created by peter on 14-9-1.
//  Copyright (c) 2014年 qiuyang. All rights reserved.
//


#import "CLCustomNavView.h"
#import "CLTabBarController.h"

@implementation CLCustomNavView


static UIViewController * gController = nil;
static CLCustomNavView * gCusNav= nil;

+ (void)defaultLeftItem:(UIViewController *)controller
                  isPop:(BOOL)isPop
         isDefineAction:(BOOL)isDefineAction
{
    gController = controller;
    gCusNav = [[CLCustomNavView alloc] init];
    
    UIButton * backButton = [[UIButton alloc] initWithFrame:CGRectMake(-10, 0, 44, 44)];
    backButton.backgroundColor = [UIColor redColor];
    [backButton setImage:[UIImage imageNamed:@"nav_back.png"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"nav_back_press.png"] forState:UIControlStateHighlighted];
    
    if (isPop && gController) {
        [backButton addTarget:gCusNav action:@selector(popAction) forControlEvents:UIControlEventTouchUpInside];
    }
    if (!isPop && !isDefineAction && gController) {
        [backButton addTarget:gCusNav action:@selector(dismissAction) forControlEvents:UIControlEventTouchUpInside];
    }
    controller.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    [controller.navigationItem setHidesBackButton:YES];
}


+ (void)rightNavItem:(UIViewController *)controller
          imageNamed:(NSString *)imageNamed
{
    gController = controller;
    UIButton * rightButton = [[UIButton alloc] initWithFrame:CGRectMake(-10, 0, 44, 44)];
    [rightButton setImage:[UIImage imageNamed:imageNamed] forState:UIControlStateNormal];
    [rightButton setImage:[UIImage imageNamed:imageNamed] forState:UIControlStateHighlighted];
    [rightButton addTarget:controller action:@selector(didClickRightNavButton) forControlEvents:UIControlEventTouchUpInside];
    controller.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}


// 在调用的Controller 里 , 必须重写改方法,不然 Crash !!!
- (void)didClickRightNavButton
{
    [NSException exceptionWithName:@"Event Error"
                            reason:@"You Must Override this Method on you controller !!!"
                          userInfo:nil];
}



- (void)popAction
{
    if (gController) {
        [gController.navigationController popViewControllerAnimated:YES];
    }
    
    if (gController.tabBarController) {
        [CLTabBarController  showTabBar:gController.tabBarController];
    }
    
    gCusNav = nil;
    gController = nil;
}


-(void)dismissAction
{
    if (gController) {
        [gController dismissViewControllerAnimated:YES completion:^{
            gCusNav = nil;
            gController = nil;
        }];
    }
}
@end
